/*
 * jngc, Java utility classes by Nicholas Coltharp
 * Copyright (C) 2010 Nicholas Coltharp <ngc1@rice.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.rice.ngc1.util;

import java.io.Serializable;

/**
 * An object used to measure events that happen in steps.
 */
public class Counter implements Cloneable, Serializable {

    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 5475783514007177120L;

    /**
     * The number that this counter is currently at.
     */
    private int value;

    /**
     * Constructs a new {@code Counter} with the given count.
     * 
     * @param value
     *            the count to start at; must be 0 or greater
     */
    public Counter(int value) {
        if (value < 0) {
            throw new IllegalArgumentException("value must be 0 or more; got "
                    + value);
        }
        this.value = value;
    }

    /**
     * Constructs a new {@code Counter} with a count of 0.
     */
    public Counter() {
        this(0);
    }

    /**
     * Constructs a new {@code Counter} with the same count as the
     * given one.
     * 
     * @param other
     *            the {@code Counter} to copy from
     */
    public Counter(Counter other) {
        this(other.value);
    }

    /**
     * Gets this {@code Counter}'s current count.
     * 
     * @return the current count
     */
    public int getValue() {
        return this.value;
    }

    /**
     * Increments the current count by 1.
     * 
     * @return the new count
     */
    public int increment() {
        return ++value;
    }

    /**
     * Sets this counter's count to 0.
     * 
     * @return 0
     */
    public int reset() {
        return (value = 0);
    }

    /**
     * Returns a new {@code Counter} whose count is equal to this
     * one's.
     * 
     * @return a new {@code Counter}
     */
    @Override
    public Counter clone() {
        return new Counter(this);
    }

    /**
     * Obeys the general contract for {@code Object#equals(Object)}.
     * 
     * Two {@code Counter}s will be compared based on their current
     * counts.
     * 
     * @param obj
     *            the object to compare for equality
     * @return {@code true} if {@code obj} is a {@code Counter} with
     *         the same count as this one; {@code false} otherwise
     */
    @Override
    public boolean equals(Object obj) {
        Counter other = EqualsHelper.check(this, obj);
        if (other == null) {
            return false;
        }
        return (other.value == this.value);
    }

    @Override
    public int hashCode() {
        return this.value;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("Counter(");
        builder.append(value);
        builder.append(")");
        return builder.toString();
    }
}
